import 'package:flutter/material.dart';
import 'package:flutter_share/apis/home.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  Map notifyDetail = {};

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 提取路由参数，从当前也买你的路由设置中获取传递的参数
    final arguments = ModalRoute.of(context)?.settings.arguments;
    // 如果参数不为空，则获取公告详情
    if (arguments != null) {
      // 获取公告详情
      getHomeDetail(arguments.toString());
    }
  }

  /// 过去公告详情
  getHomeDetail(String id) async {
    // 请求公告详情
    final res = await HomeApi.getHomeDetailList(id);
    // 更新数据
    setState(() {
      notifyDetail = res;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('公告详情'),
      ),
      body: ListView(
        children: [
          Container(
            padding: const EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 标题
                Text(
                  notifyDetail['title'] ?? '',
                  style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 10),
                Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        notifyDetail['creatorName'] ?? '',
                        style: const TextStyle(
                          color: Colors.grey,
                        ),
                      ),
                      Text(
                        notifyDetail['createdAt'] ?? '',
                        style: const TextStyle(
                          color: Colors.grey,
                        ),
                      )
                    ]),
                const SizedBox(height: 10),
                // 内容
                HtmlWidget(
                  notifyDetail['content'] ?? "",
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
